package com.example.springbootdemo1.controller;

import com.sun.javafx.collections.MappingChange;
import org.apache.catalina.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import sun.awt.SunHints;

import java.util.HashMap;
import java.util.Map;


/*
* 返回ModelAndView
* 通过ModelAndView可以带如数据，并渲染到页面
* @return
* */
@RestController
public class CodingStudyIndexController {
    @RequestMapping("/index")
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView();
        //可以设置参数
        String msg="灰常棒";

        modelAndView.setViewName("index");
        modelAndView.addObject("msg",msg);
        return modelAndView;
    }

    /*
    * 假设是获取用户信息
    * @return
    * */
    @RequestMapping("/user")
    @ResponseBody
    public Map<String,Object> getUser(){
        //模拟一个用户
        Map<String,Object> user = new HashMap<>();
        user.put("id",1);
        user.put("name","张三");
        user.put("age",18);
        //返回这个用户
        return user;
    }

    @RequestMapping("/test")
    public ModelAndView print(){
        ModelAndView mv = new ModelAndView();
        mv.addObject("data","123654");
        mv.setViewName("test");
        return mv;
    }

    /*
    * RequestParam:返回参数
    * value:请求参数名（必须配置）
    * required（必须的）：是否必须，默认为true，即 请求中必须包含该参数，如果不包含就会抛异常
    * defaultValue(默认值): 默认值，如果设置了该值，required将自动设为false,无论你是否配置了reauired配置什么纸都是默认为false
    * */
    @RequestMapping("/param")
    @ResponseBody
    public String param(
            @RequestParam("id")String id,
            @RequestParam(value = "name",required = false) String name,
            @RequestParam(value = "age",required = false) int age
    ){
        String data="";
        try{
            //返回ID，NAME，AGE
            data="id"+id+",name="+name+",age"+age;
        }catch (Exception e){
            data = e.getMessage();
        }
        return data;
    }


    /*
    *
    * @PathVariable: 路径变量
    * value:请求参数名
    * required：是否必需，默认为true,即 请求中必须包含该参数，如果没有包含将会抛异常
    * */
    @GetMapping("/path/{id}/{name}")
    @ResponseBody
    public String pathVar(
            @PathVariable("id") String id,
            @PathVariable(value = "name",required = false) String userName
    ){
        return "id="+id+"  name="+userName;
    }


    /*
    * @GetMapping :组合注解，是requestMapping(method = RequestMethod.GET)的缩写，只接受前端的get请求
    * */
    @GetMapping("/get")
    public Map<String,Object> getUser1(){
        Map<String,Object> map = new HashMap<>();
        map.put("id",1);
        map.put("name","十七");
        return map;
    }

    /*
    * @postMapping :组合注解，是requestMapping(method = RequestMethod.POST)的缩写，只接受前端的post请求,一般用于数据的新增
    * */
    @PostMapping
    public boolean insert(@RequestBody User user){
        //insert
        return true;
    }

    /*
    * @DeleteMapping :组合注解，是requestMapping(method = RequestMethod.delete)的缩写，只接受前端的delete提交，用于数据删除
    * */
    @DeleteMapping
    public boolean del(@PathVariable("id") Object id){
        System.out.println(id);
        //delete
        return true;
    }


}
